package org.ejde.sciencelogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * This class contains the attributes and behaviors for main panel of the
 * Periscope project. The main panel is is where all of the fields, global
 * information and additonal text are displayed.
 * 
 * mainPanel uses a borderLayout layout manager. The top panel contains the
 * global information such as DIVE ID, CRUISE ID, etc. The center panel contains
 * all of the fields. The right panel contains the list of all online buddies
 * (only if XMPP enabled). The bottom panel is used for adding additional text.
 * There is no left panel.
 * 
 * @author Eric Martin
 * @version 0.1
 */
public class mainPanel extends JPanel {

	/**
	 * The center pane; Contains the tabs for each panel of formFields.
	 */
	public tabPane tp;

	/**
	 * The top panel; Contains the global information such as DIVE ID, CRUISE
	 * ID, etc.
	 */
	public JPanel top;

	/**
	 * The bottom panel; Contains a JTextArea widget for adding additional text.
	 */
	public JPanel bot;

	/**
	 * Constructor method. Creates the main panel.
	 * 
	 * @param ini
	 *            configuration information
	 * @param factory
	 *            information on all fields
	 * @param udp
	 *            the UDP client for doing UDP-based logging.
	 * @param smack
	 *            the XMPP-based client for doing XMPP-based logging.
	 */
	public mainPanel(iniParser ini, fieldFactory factory, UdpXmitter udp,
			smackClient smack) {

		// Retrieve a formField array containing just the
		// formFields to be added to the top panel
		formField[] tpf = factory.getTopPanelFields();

		// Verify one of the formFields for the top panel
		// is of type JTextArea
		boolean flag = false;
		for (int n = 0; n < tpf.length; n++) {
			if (tpf[n].type == formField.TEXTAREA_FIELD) {
				flag = true;
			}
		}

		// If no type JTextArea formFields are found,
		// return an error.
		if (!flag) {
			JOptionPane.showMessageDialog(factory.frame,
					"No Note Panel Defined", "Missing Field",
					JOptionPane.ERROR_MESSAGE);
			// TODO Should this cause the application to quit?
		}

		// Build the pane. Add the top and bottom panels.
		this.setLayout(new BorderLayout());
		top = new JPanel(new FlowLayout());
		bot = new JPanel(new FlowLayout());

		// Add the formFields to the top panel
		for (int n = 0; n < tpf.length; n++) {
			// Get the appropriate action
			switch (tpf[n].type) {
			case formField.TEXT_FIELD:
				top.add(tpf[n].getLabel());
				top.add(tpf[n].getTextField());
				break;
			case formField.LIST_FIELD:
				top.add(tpf[n].getLabel());
				top.add(tpf[n].getListField());
				break;
			case formField.TEXTAREA_FIELD:
				bot.add(tpf[n].getLabel());
				bot.add(tpf[n].getTextAreaField());
			}
		}

		// Add the stats panel for the xmmp server if enabled
		if (ini.getXmppEnabled() == 1) {
			smackStatPanel smackstats = new smackStatPanel(ini, smack);
			smack.setSmackstatpanel(smackstats);
			bot.add(smackstats);
		}

		// Add top and bottom panel to borderLayout
		this.add("North", top);
		this.add("South", bot);

		// Add tabbed pane to center panel of the
		// borderlayout.
		tp = new tabPane(ini, factory, udp, smack);
		this.add("Center", tp);

		
		// Add XMPP user list (only if XMPP-enabled).
		if (ini.getXmppEnabled() == 1) {
			smackUserList userlist = new smackUserList(ini, smack);
			smack.setSmackuserlist(userlist);
			this.add("East", userlist);
		}

	}
}
